package com.homework;

public class Homework06 {
    public static void main(String[] args) {
        Person tang = new Person("唐僧", new Horse());
        tang.Comment();
        tang.crossRiver();
        tang.Comment();
        tang.crossRiver();
        tang.Comment();
        tang.crossRiver();

        tang.crossFire();
        tang.Comment();
        tang.crossFire();
        tang.Comment();
        tang.crossFire();
        tang.crossRiver();
        tang.crossRiver();
    }
}

interface Vehicles {
    void work();
}

class VehicleFactory {
    private static Horse horse = new Horse();

    private VehicleFactory() {}

    public static Boat getBoat() {
        return new Boat();
    }

    public static Horse getHorse() {
        return horse;
    }

    public static Plane getPlane() {
        return new Plane();
    }
}

class Horse implements Vehicles {

    @Override
    public void work() {
        System.out.println("一般情况骑马");
    }
}

class Boat implements Vehicles {

    @Override
    public void work() {
        System.out.println("遇到大河坐船");
    }
}
class Plane implements Vehicles {

    @Override
    public void work() {
        System.out.println("遇到火焰山坐飞机");
    }
}

class Person {
    private String name;
    private Vehicles Vehicle;


    public Person(String name, Vehicles Vehicle) {
        this.name = name;
        this.Vehicle = Vehicle;
    }


    public void Comment() {
        if (!(Vehicle instanceof Horse)) {
            Vehicle =  VehicleFactory.getHorse();
        }
        Vehicle.work();
    }

    public void crossRiver() {
        if ( !(Vehicle instanceof Boat)) {
            Vehicle = VehicleFactory.getBoat();
        }
        Vehicle.work();
    }

    public void crossFire() {
        if (! (Vehicle instanceof Plane)) {
            Vehicle = VehicleFactory.getPlane();
        }
        Vehicle.work();
    }

}

